$(function () {
    //选中样式
    $('.label-nav-con li').click(function() {
        $(this).addClass('current').siblings().removeClass('current');
        var buildingNo = $(this).attr("id");
        changeFloorInfo(buildingNo);
        queryOldManInfo();
    })
    //查询老人信息
    queryOldManInfo();
})

//切换楼层
function changeFloorInfo(buildingNo){
    if(buildingNo==''){
        var str="<option value=''>请选择楼层</option>";
        $("#floorSelect").empty();
        $("#floorSelect").append(str);
        return false;
    }
    $.ajax({
        url:getRootPath_web()+"baseData/floor/obtainFloorData",
        type:'post',
        dataType:'json',
        data:{"buildingNo":buildingNo},
        success:function (result) {
            if(result.floorCount>0){
                var str="<option value=''>请选择楼层</option>";
                $.each(result.data,function (key,f) {
                    str+="<option value='"+f.floorNo+"'>"+f.floorName+"</option>";
                })
                $("#floorSelect").empty();
                $("#floorSelect").append(str);
            }
        }
    })
}

function clearSelectPara() {
    //获取搜索条件
    $("#floorSelect").val("");
    $("#roomName").val("");
    $("#bedName").val("");
    $("#userName").val("");
    $("#archiveFlag").val("");
}
//搜索老人信息
function queryOldManInfo() {
    //获取搜索条件
    var buildingNo =  $('.current').attr("id");
    var floorNo =$("#floorSelect").val();
    var roomName =$("#roomName").val();
    var bedName =$("#bedName").val();
    var userName=$("#userName").val();
    var archiveFlag=$("#archiveFlag").val();
    layui.use(['element','table'], function() {
        var table = layui.table;
        table.render({
            elem: '#test1'
            ,url: getRootPath_web()+'/healthArchive/elderly/pagedQueryOldManData',
            method:'post',
            where:{buildingNo:buildingNo, floorNo:floorNo
                ,roomName:roomName,bedName:bedName
                ,userName:userName,archiveFlag:archiveFlag},
            request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
            },
            parseData: function(res){
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.codeDesc, //解析提示文本
                    "count": res.totalRecord, //解析数据长度
                    "data": res.data //解析数据列表
                };
            },
            done:function(res){
              $("#totalRecord").html(res.count);
            },
            page: true //开启分页
            ,
            cols: [
                [ //表头
                    {
                        field: 'headUl',
                        title: '照片',
                        width: 80,
                        templet: function(res){
                            if(res.headUl!=null&&res.headUl!=''){
                                return "<div class='table-img'>" +
                                    "<span><img src='"+res.headUl+"'></span></div>"
                            }else {
                                return "<div class='table-img'>" +
                                    "<span><img src='"+getRootPath_web()+"static/img/defaultHead.jpg'></span></div>"
                            }
                        }
                    }, {
                    field: 'userName',
                    title: '姓名',
                }, {
                    field: 'userSex',
                    title: '性别',
                }, {
                    field: 'buildingName',
                    title: '楼苑',
                }, {
                    field: 'roomName',
                    title: '房间号',
                }, {
                    field: 'bedName',
                    title: '床号',
                    width: 110,

                }, {
                    field: 'symptom',
                    title: '病症',
                }, {
                    field: 'checkDate',
                    title: '上次查房时间',
                }, {
                    field: 'city',
                    title: '',
                    width:150,
                    templet: function(res){
                        var overHour = res.overHour;
                        var isArchive = res.isArchive;
                        if(isArchive==0){
                            if(overHour>0){
                                if(overHour<12){
                                    return "<div class='table-list-age'><span class='span-bgYellow'>还剩"+overHour+"小时</span></div>";
                                }else if(overHour<=48){
                                    return "<div class='table-list-age'><span class='span-bgGreen'>还剩"+overHour+"小时</span></div>";
                                }
                            }else {
                                return "<div class='table-list-age'><span class='span-bgRed'>还剩0小时</span></div>";
                            }
                        }else {
                            return "<div class='table-list-age'><span></span></div>";
                        }

                    }
                }, {
                    field: 'city',
                    title: '操作',
                    style: 'color:#1C9399;',
                    fixed: 'right',
                    width: 140,
                    templet: function(res){
                        var isArchive = res.isArchive;
                        console.log("true+++="+isCURD);
                        if(isCURD){
                            if(isArchive=='0'){
                                return "<div class='table-list-btn'><span lay-event='add' class='cursor'>创建档案</span></div>"
                            }else {
                                return "<div class='table-list-btn'><span lay-event='detail'  class='cursor'>查看报告</span></div>"
                            }
                        }else{
                            if(isArchive=='0'){
                                return "<div class='table-list-btn'><span  style='color: gray'>创建档案</span></div>"
                            }else {
                                return "<div class='table-list-btn'><span  style='color: gray'>查看报告</span></div>"
                            }
                        }
                    }

                }
                ]
            ]
        });
        //点击事件
        table.on('tool(test)', function(obj){
            var data = obj.data;
            var userNo = data.userNo;
            if(obj.event === 'add'){
                addHealthArchive(userNo);
            } else if(obj.event === 'detail'){
                checkHealthArchive(userNo);
            }
        });
    });

    $('.more-date div.add-mess-flex:gt(1)').hide();
    $('.btn-select').click(function() {
        if ($(this).find('img').attr('src') == 'img/select_icon_down.png') {
            $(this).find('img').attr('src', 'img/select_icon_up.png').siblings("span").text("收起");

        } else {
            $(this).find('img').attr('src', 'img/select_icon_down.png').siblings("span").text("展开");

        }
        $('.more-date div.add-mess-flex:gt(1)').slideToggle(300);
    })
}
//创建健康档案
function  addHealthArchive(userNo) {
    window.location.href=getRootPath_web()+"healthArchive/elderly/addBaseHealthy?userNo="+userNo;
}
//查看健康档案
function checkHealthArchive(userNo) {
    window.location.href=getRootPath_web()+"healthArchive/elderly/healthArchiveDetail?userNo="+userNo;
}

